/* SVN.committedRevision=c2b13f9 */
var myCommon = {
    backTop: function () {
        var b = $(".prism_to_top");
        $(window).scroll(function () {
            var a = $(window).scrollTop();
            if (a > 0) {
                b.fadeIn(500)
            } else {
                b.fadeOut(500)
            }
        });
        $(".prism_to_top").on("click", ".back_top", function () {
            $("body,html").animate({scrollTop: 0}, 300)
        })
    }, myHeader: function () {
        function b(a) {
            $(a).hover(function () {
                $(this).addClass("active");
                $(this).find(".arrow_icon").html("&#xe678;")
            }, function () {
                $(this).removeClass("active");
                $(this).find(".arrow_icon").html("&#xe62f;")
            })
        }

        b(".nav_item");
        $(".hd_header_search").on("focus", ".enter_box", function () {
            $(this).parents(".hd_header_search").addClass("focusd")
        }).on("blur", ".enter_box", function () {
            $(this).parents(".hd_header_search").removeClass("focusd")
        })
    }, sideNav: function () {
        $(".mpc_cont_sideNav dl").hover(function () {
            $(this).addClass("selected").siblings("dl").removeClass("selected")
        }, function () {
            $(this).removeClass("selected")
        })
    }, init: function () {
        this.backTop();
        this.myHeader();
        this.sideNav()
    }
};
myCommon.init();
var myOrder2018 = {
    showAddToCartBox: function (d) {
        var c = $(".buy-btn");
        c.click(function () {
        })
    }, selectOrderType: function () {
        var b = $(".tit_tab .title");
        b.on("click", function () {
            $(this).addClass("cur");
            $(this).siblings().removeClass("cur")
        })
    }, initTimeout: function () {
        var b = $(".count-down");
        this.setTime(b, 0);
        this.countTime(b)
    }, countTime: function (d) {
        var c = this;
        clearTimeout(this.timeCount);
        this.timeCount = setTimeout(function () {
            c.setTime(d, 1);
            c.countTime(d)
        }, 1000)
    }, setTime: function (d, e) {
        var f = this;
        d.each(function (c, a) {
            var b = f.formatTime($(a).attr("data-leftTime"), e);
            $(a).attr("data-leftTime", b[0]);
            $(a).find(".timeout").text(b[1]);
            if (b[0] == 0) {
                f.timeEnd()
            }
        })
    }, formatTime: function (i, f) {
        i = parseInt(i);
        if (!i || i < 0) {
            i = 0
        }
        i = Math.round(i / 1000);
        if (i > 0) {
            i -= f
        }
        var g = ("0" + Math.floor(i / 3600)).substr(-2);
        var j = ("0" + Math.floor(i % 3600 / 60)).substr(-2);
        var h = ("0" + Math.floor(i % 3600 % 60)).substr(-2);
        return [i * 1000, g + " : " + j + " : " + h]
    }, timeEnd: function () {
        if (this.hasEnd) {
            return
        }
        this.hasEnd = true;
        console.log("倒计时结束，这里请求数据，只触发一次")
    }, init: function () {
        this.showAddToCartBox("failed");
        this.selectOrderType();
        this.initTimeout()
    }
};
myOrder2018.init();
$(function () {
    checkVenderServiceStatus();
    loadPgBar()
});

function loadPgBar() {
    var m = $("#currentPage").val();
    var i = $("#pageSize").val();
    var j = $("#totalCount").val();
    console.log("init-param, currentPage=" + m + ", pageSize=" + i + ", totalCount=" + j);
    if (!m || !i || !i) {
        return
    }
    m = Number(m);
    i = Number(i);
    j = Number(j);
    var n = parseInt((j + i - 1) / i + "");
    console.info("totalPage=" + n);
    var k = new Array(8);
    if (n <= 8) {
        for (var o = 0; o < n; o++) {
            k[o] = o + 1
        }
    } else {
        if (m <= 4) {
            k[0] = 1;
            k[1] = 2;
            k[2] = 3;
            k[3] = 4;
            k[4] = 5;
            k[5] = 6;
            k[6] = 7;
            k[7] = "...";
            k[8] = n
        } else {
            if (m > 4 && m < n - 4) {
                k[0] = m - 3;
                k[1] = m - 2;
                k[2] = m - 1;
                k[3] = m;
                k[4] = m + 1;
                k[5] = m + 2;
                k[6] = m + 3;
                k[7] = "...";
                k[8] = n
            } else {
                k[0] = 1;
                k[1] = "...";
                k[2] = n - 6;
                k[3] = n - 5;
                k[4] = n - 4;
                k[5] = n - 3;
                k[6] = n - 2;
                k[7] = n - 1;
                k[8] = n
            }
        }
    }
    var p = "";
    if (1 == m) {
        p += '<span class="page_start"></span>'
    } else {
        p += '<a class="page_prev"></a>'
    }
    for (var o = 0; o < k.length; o++) {
        if (!k[o]) {
            break
        }
        if (m == k[o]) {
            p += '<span class="page_cur">' + m + "</span>"
        } else {
            if ("..." == k[o]) {
                p += '<span class="page_break">...</span>'
            } else {
                p += '<a class="page_nav_item">' + k[o] + "</a>"
            }
        }
    }
    p += '<em class="font_tit">共' + n + '页 到第<input class="sum_text" type="text" value="" id="jumpto" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,\'\');}).call(this)" onblur="this.v();"/><i class="sum_btn">确定</i></em>';
    if (n == m) {
        p += '<span class="page_end"></span>'
    } else {
        p += '<a class="page_next"></a>'
    }
    $(".turnPageBottom").empty();
    $(".turnPageBottom").html(p);
    var l = $(".hd_order_list > p.hover").attr("data-orderStatus");
    $(".page_prev").bind("click", function () {
        var a = m - 1;
        console.log("pagePrev=" + a);
        if (a > 0) {
            pgOrderList(a)
        }
    });
    $(".page_next").bind("click", function () {
        var a = m + 1;
        console.log("pageNext=" + a);
        if (a <= n) {
            pgOrderList(a)
        }
    });
    $(".page_nav_item").bind("click", function () {
        var a = $(this).text();
        pgOrderList(a)
    });
    $(".sum_btn").bind("click", function () {
        var a = $.trim($(".sum_text").val());
        if (!a) {
            return
        }
        a = Number(a);
        if (n < a) {
            a = n;
            $(".sum_text").val(a)
        }
        pgOrderList(a)
    })
}

function pgOrderList(d) {
    var c = $(".tit_tab").find(".cur").attr("data-orderStatus");
    $.ajax({
        type: "POST",
        url: "/order/orderListPg.do?page=" + d + "&orderStatus=" + c,
        async: false,
        dataType: "html",
        success: function (a) {
            $(".orders-all").html(a);
            myOrder2018.initTimeout();
            checkVenderServiceStatus();
            loadPgBar()
        },
        error: function (f, a, b) {
            window.location.href = "//home.yhd.com/order/toOrderList.do"
        }
    })
}

function pgOrderList2(b) {
    $.ajax({
        type: "POST",
        url: "/order/orderListPg.do?page=1&orderStatus=" + b,
        async: false,
        dataType: "html",
        success: function (a) {
            $(".orders-all").html(a);
            myOrder2018.initTimeout();
            checkVenderServiceStatus();
            loadPgBar()
        },
    })
}

function checkVenderServiceStatus() {
    $(".vanderCode").each(function () {
        var d = $(this).val();
        var e = $(this).attr("order");
        if (d == 4) {
            return
        }
        var f = "//chat1.jd.com/api/checkChat?venderId=" + d + "&returnCharset=utf-8";
        $.ajax({
            url: f, dataType: "jsonp", jsonp: "callback", success: function (a) {
                if (a.code != 1) {
                    $("#chat_show_" + e).addClass("server-disable")
                }
            }, error: function () {
                console.log("校验客服在线状态失败")
            }
        })
    })
}

function checkChat(d, c) {
    $.ajax({
        type: "POST", url: "/ordercenter/checkChat.do", dataType: "json", success: function (h) {
            if (null != h) {
                if (h.code == 0) {
                    var a = null;
                    if (c == null || c.length == 0 || c == 0) {
                        c = 4
                    }
                    if (c == 4) {
                        a = "jd_web_h_yhd"
                    } else {
                        a = "jd_web_yhd_item"
                    }
                    var b = "https://chat.jd.com/index.action?venderId=" + c + "&orderId=" + d + "&customerAppId=yhd.customer&entry=" + a + "&loginToken=" + h.result.loginToken + "&fp=" + h.result.fp + "&aspid=" + h.result.aspid;
                    window.open(b)
                } else {
                    if (h.code == 1) {
                        var g = window.location.href;
                        window.location.href = "https://passport.yhd.com/passport/login_input.do?returnUrl=" + g
                    }
                }
            } else {
                $("#errorMsg").text("请求异常,稍后重试");
                $("#errbox").removeClass("hide")
            }
        }
    })
}

function addItemsBySku(e, d) {
    console.log("加车");
    var f = $(d).siblings(".addotcart-con");
    $.ajax({
        type: "POST",
        url: "http://cart.m.yhd.com/cart/opt/add.do?pid=" + e + "&pcount=1&ptype=1",
        dataType: "jsonp",
        success: function (b) {
            if (null != b) {
                if (b.code == "00000000") {
                    f.addClass("addotcart-success");
                    f.find(".addtocart-text-tip").text("成功加入购物车")
                } else {
                    if (b.code == "60000001") {
                        f.addClass("addotcart-success");
                        f.find(".addtocart-text-tip").text("无效商品")
                    } else {
                        if (b.code == "60000003") {
                            f.addClass("addotcart-success");
                            f.find(".addtocart-text-tip").text("商品无库存")
                        } else {
                            f.addClass("addotcart-failed");
                            f.find(".addtocart-text-tip").text("系统繁忙请稍后")
                        }
                    }
                }
                $(d).siblings(".addotcart-con").show();
                var a = $(d);
                clearTimeout(this.timer);
                this.timer = setTimeout(function () {
                    a.siblings(".addotcart-con").hide()
                }, 2000)
            } else {
                f.addClass("addotcart-failed");
                f.find(".addtocart-text-tip").text("系统繁忙请稍后");
                $(d).siblings(".addotcart-con").show();
                var a = $(d);
                clearTimeout(this.timer);
                this.timer = setTimeout(function () {
                    a.siblings(".addotcart-con").hide()
                }, 2000)
            }
        }
    })
};